package com.hmyd.hmyd_android.fragment;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hmyd.hmyd_android.R;
import com.hmyd.hmyd_android.adapter.RecommendBonusListAdapter;
import com.hmyd.hmyd_android.commons.UrlConstant;
import com.hmyd.hmyd_android.dto.Recommend;
import com.hmyd.hmyd_android.dto.RecommendPage;
import com.hmyd.hmyd_android.view.listview.XListView;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class RecommendBonusListFragment extends BasicFragmentList {
	
	private XListView mList;
	private RecommendBonusListAdapter mRecommendListAdapter;
	
	private final int REFRESH = 1;
	private final int LOADMORE = 2;
	
	protected boolean hasMore = false;
	private String cursorId;
	
	private List<Recommend> listData = new ArrayList<Recommend>();
	
	private TextView totalAlreadyRewardAmount;
	private TextView totalExpectRewardAmount;
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View investmentLayout = inflater.inflate(R.layout.fragment_recommend_bonus_list, container, false);
		
		initViews(investmentLayout);
		initListener();
		
		return investmentLayout;
	}
	
	public XListView getXList() {
		return mList;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		if (!isLoading && listData.size() == 0) {
			onRequest(REFRESH, UrlConstant.RECOMMENDBONUS, HttpMethod.GET, null);
		}
	}
	
	
	private void initViews(View rootLayout){
		mList = (XListView) rootLayout.findViewById(R.id.list);
		mList.setPullRefreshEnable(true); // 允许下拉刷新
		mList.setPullLoadEnable(false); // 禁止加载更多
		mList.setAutoLoadEnable(false); // 禁止自动加载
		mList.setXListViewListener(this);// 加载监听
		
		mRecommendListAdapter = new RecommendBonusListAdapter(getActivity(), listData);
		mList.setAdapter(mRecommendListAdapter);
		
		totalAlreadyRewardAmount = (TextView)rootLayout.findViewById(R.id.totalAlreadyRewardAmount);
		totalExpectRewardAmount = (TextView)rootLayout.findViewById(R.id.totalExpectRewardAmount);
	}
	
	@Override
	public void onRefresh() {
		super.onRefresh();
		onRequest(REFRESH, UrlConstant.RECOMMENDBONUS, HttpMethod.GET, null);
	}

	@Override
	public void onLoadMore() {
		super.onLoadMore();

		RequestParams params = new RequestParams();
		params.addBodyParameter("lastCursor", cursorId);
		
		onRequest(LOADMORE, UrlConstant.RECOMMENDBONUS, HttpMethod.GET, params);
	}
	
	public void onPostSuccess(int postId, String result) {
		super.onPostSuccess(postId, result);
		
		Gson gson = new Gson();
		java.lang.reflect.Type type2 = new TypeToken<RecommendPage>() {}.getType();
		RecommendPage page = gson.fromJson(result, type2);
		
		if(page!=null && page.bonusList!=null && page.bonusList.records!=null){
			hasMore = page.bonusList.hasMore;
			
			switch (postId) {
			case REFRESH:
				listData.clear();
				listData.addAll(page.bonusList.records);
				break;
			case LOADMORE:
				listData.addAll(page.bonusList.records);
				break;
			default:
				break;
			}
			
			if(listData.size()>0){
				cursorId = listData.get(listData.size()-1).cursorId;
			}else{
				cursorId = null;
			}
			
			mRecommendListAdapter.notifyDataSetChanged();
			
			totalAlreadyRewardAmount.setText(page.totalAlreadyRewardAmount);
			totalExpectRewardAmount.setText(page.totalExpectRewardAmount);
		}
		
		if(getXList()!=null && hasMore){
			getXList().setPullLoadEnable(true);
		}else if(getXList()!=null && !hasMore){
			getXList().setPullLoadEnable(false);
		}
	}

	public void onPostFailure(int postId, String msg) {
		super.onPostFailure(postId, msg);

		if(getXList()!=null && hasMore){
			getXList().setPullLoadEnable(true);
		}else if(getXList()!=null && !hasMore){
			getXList().setPullLoadEnable(false);
		}
	}
	
	private void initListener(){
		mList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//				startActivity(new Intent(getActivity(),InvestmentDetailActivity.class));
			}
		});
	}
	
	
}
